package com.changgou.page.service;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private SpuFeign spuFeign;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private CategoryFeign categoryFeign;

    @Autowired
    private TemplateEngine templateEngine;

    @Value("${pagepath}")
    private String pagepath;


    @Override
    public Map buildPageData(String spuId) {
        Map pageData = new HashMap();

        //1.根据spuId查询到spu
        Spu spu = spuFeign.findById(spuId);
        if(spu!=null){
            pageData.put("spu", spu);

            //2.根据spuId查询到sku列表
            List<Sku> skuList = skuFeign.findBySpuId(spuId);
            pageData.put("skuList", skuList);


            //3.根据spu的三级分类ID获取三条分类数据
            Category category1 = categoryFeign.findById(spu.getCategory1Id());
            Category category2 = categoryFeign.findById(spu.getCategory2Id());
            Category category3 = categoryFeign.findById(spu.getCategory3Id());
            pageData.put("category1", category1);
            pageData.put("category2", category2);
            pageData.put("category3", category3);

            //4.根据spu获取图片链接列表
            if(StringUtils.isNotEmpty(spu.getImages())){
                List<Map> maps = JSON.parseArray(spu.getImages(), Map.class);
                if(maps!=null && maps.size()>0){
                    List<String> imageList = new ArrayList<>();
                    for (Map map : maps) {
                        imageList.add(String.valueOf(map.get("url")));
                    }
                    pageData.put("imageList", imageList);
                }

            }

            //5.根据spu获取所有商品的规格列表
            if(StringUtils.isNotEmpty(spu.getSpecItems())){
                Map specificationList = JSON.parseObject(spu.getSpecItems(), Map.class);
                pageData.put("specificationList", specificationList);
            }

            
        }


        return pageData;
    }

    @Override
    public void createPageHtml(String spuId) {
        //定义模板引擎的上下文实例
        Context context = new Context();
        //设置今天页模板所需要的的数据
        Map pageData = buildPageData(spuId);
        context.setVariables(pageData);

        //判断静态页生成的目标目录是否存在，不存在就创建
        File htmlDir = new File(pagepath);
        if(!htmlDir.exists()){
            htmlDir.mkdirs();
        }

        //利用模板引擎实例生成静态页面到指定的位置
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(new File(pagepath + "/"+ spuId + ".html" ));
            templateEngine.process("item", context,pw );
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(pw!=null){
                pw.close();
            }
        }

    }


    @Override
    public void deletePageHeml(String spuId) {
        File file = new File(pagepath+"/"+spuId+".html");
        if(!file.exists()){
           throw new RuntimeException("文件不存在！");
        }
        file.delete();
    }
}
